package com.project.biz.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.biz.CmsProductSpecBiz;
import com.project.entity.CmsProductSpec;
import com.project.mapper.CmsProductSpecMapper;
import com.project.pojo.model.UpdateProductSpecModel;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 商品规格业务层接口实现类
 * @Author: zhuchi
 * @CreateDate: 2020/10/15 15:12
 * @Version: 1.0
 */
@Service
public class CmsProductSpecBizImpl extends ServiceImpl<CmsProductSpecMapper, CmsProductSpec> implements CmsProductSpecBiz {

    /**
     * 添加/修改商品规格
     * @param productSpecs
     * @param productId
     * @return
     */
    public int updateProductSpec(List<UpdateProductSpecModel> productSpecs, Long productId){
        //清空现有商品规格
        QueryWrapper<CmsProductSpec> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id", productId);
        this.baseMapper.delete(queryWrapper);

        if (null != productSpecs && productSpecs.size() > 0){
            int insertProductSpec = this.baseMapper.insertProductSpec(productSpecs, productId);
            return insertProductSpec;
        }

        return 0;
    }

    /**
     * 获取商品规格
     * @param productId
     * @return
     */
    public List<CmsProductSpec> getProductSpecs(Long productId){
        QueryWrapper<CmsProductSpec> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id", productId);
        List<CmsProductSpec> list = this.baseMapper.selectList(queryWrapper);

        return list;
    }

}
